package com.hymandev.goodadpter.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hymandev.goodadpter.R;
import com.hymandev.goodadpter.bean.Book;

import java.util.List;

/**
 * Desc:
 * Created by Hym on 2015/12/4 15:01.
 * Modified by
 * Modification：
 */
public class MyAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context mContext;
    private List<Book> mBooks;

    public MyAdapter(Context context, List<Book> books) {
        this.mContext = context;
        this.mBooks = books;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mBooks.size();
    }

    @Override
    public Object getItem(int position) {
        return mBooks.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /////////////////////////////getView优化//////////////////////////////////
    //基本优化：
    //①使用convertView重用，避免每次getView方法的时候，都去创建新的View
    //②使用ViewHolder来临时存储item中的控件，避免出现重复多次的findViewById，此操
    //作也是消耗内存的操作
    //③ListView的在布局中，不要设置自适应(wrap_content),这样会导致getView方法的重
    //调用
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.lv_books_items, parent, false);
            viewHolder.tv_desc = (TextView) convertView.findViewById(R.id.tv_desc);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
            //setTag方法学习
            stu4ViewPsetTag();
            convertView.setTag(viewHolder);

        }else{
            viewHolder=(ViewHolder)convertView.getTag();
        }
        Log.e("convertView","当前的position："+position+"当前的对象："+convertView.toString());
        return convertView;
    }

    class ViewHolder {
        private TextView tv_title;
        private TextView tv_desc;
        private TextView tv_price;
    }
    //////////////////////////////以下是辅助理解代码///////////////////////////////

    /**
     * 空方法，用于理解View.setTag()
     */
    private void stu4ViewPsetTag() {
        /*****************************setTag方法理解******************************/
        //Sets the tag associated with this view. A tag can be used to mark a view
        //in its hierarchy and does not have to be unique within the hierarchy.
        // Tags can also be used to store data within a view without resorting to
        // another data structure.
        //中文：设置一个和当前View关联的标记。这个标记可以用来标示一个view在它的层级中
        //但是有不要求其在层级中的唯一性。同时标记可以在View内部用于存储数据而无需重新
        //存储另一个数据结构
        /************************************************************************/
    }
}
